/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.connor.bank.Resource;

import com.connor.bank.Entity.DatabaseConnection;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.InvalidPropertiesFormatException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author raytang
 */
public class ResourceHandler {

    private static final String USERPROFILE = "src/properties/UserProfile.xml";
    private static final String USERPROFILE_SQL = "SELECT * FROM CUSTOMERSINFO WHERE LOGINUSERNAME = '%s';";
    private static final String ACCOUNT = "src/properties/Accounts.xml";
    private static final String ACCOUNT_SQL = " ";
    private static final String ADMIN_FUNCTION = "src/properties/adminFunction.xml";
    private static final String USER_FUNCTION = "src/properties/userFunction.xml";

    public static void createProperties(String type, String username) {
        String filePath = getFilename(type);
        String sql = String.format(getSQL(type), username);
        
        File file = new File(filePath);
        if (file.exists())
            file.delete();
        Properties pros = new Properties();
        DatabaseConnection db = new DatabaseConnection();
        ResultSet rs = db.executeQuery(sql);
        try {
            ResultSetMetaData metadata = rs.getMetaData();
            rs.next();
            for (int i = 1; i <= metadata.getColumnCount(); i++) {
                pros.setProperty(metadata.getColumnLabel(i), rs.getString(i));
            }
            FileOutputStream fout = new FileOutputStream(filePath);
            pros.storeToXML(fout, null);
        } catch (SQLException | IOException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.closeConnection();
        String id = getValue("userprofile", "CUSTOMERID");
        String role = getValue("userprofile", "Role");
        Session.addAttribute("id", id);
        Session.addAttribute("role", role);
    }

    private static String getSQL(String type) {
        switch (type.toLowerCase()) {
            case "userprofile":
                return USERPROFILE_SQL;
            case "accounts":
                return ACCOUNT_SQL;
            default:
                return null;
        }
    }

    private static String getFilename(String type) {
        switch (type.toLowerCase()) {
            case "userprofile":
                return USERPROFILE;
            case "accounts":
                return ACCOUNT;
            case "adminfunction":
                return ADMIN_FUNCTION;
            case "userfunction":
                return USER_FUNCTION;
            default:
                return null;
        }
    }

    private static Properties getPropertiesFile(String type) {
        try {
            File file = new File(getFilename(type));
            Properties pros = new Properties();
            try (FileInputStream ins = new FileInputStream(file)) {
                pros.loadFromXML(ins);
            }
            return pros;
        } catch (InvalidPropertiesFormatException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public static String getValue(String type, String key) {
        return getPropertiesFile(type).getProperty(key);
    }

    public static Map<String, String> getAllValue(String type) {
        Properties pros = getPropertiesFile(type);
        Map<String, String> result = new LinkedHashMap<>();
        for (Map.Entry<Object, Object> map : pros.entrySet()) {
            result.put((String) map.getKey(), (String) map.getValue());
        }
        return result;
    }

    public static void delete() {
        new File(USERPROFILE).delete();
    }
}

    /*
    public static void createProperties(String username) {
        FileOutputStream fouts;
        
        Properties pros = null;
        if(new File(FILE_NAME).exists()){
            try {
                ResourceBundle rb = ResourceBundle.getBundle(FILE_NAME);
                pros.load(new FileInputStream(FILE_NAME));
            } catch (IOException ex) {
                Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        try {
            Properties properties = new Properties();
            properties.setProperty("username", username);
            fouts = new FileOutputStream(FILE_NAME);
            properties.store(fouts, null);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    * */